package Menu;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by 琪 on 2017/2/23.
 */
public class CardRotateMenu extends ViewGroup implements View.OnClickListener {

    private View MainIcon;
    private enum isOpen {
        OPEN,CLOSE
    }
    private enum isAnimation{
        RUNNING,STOPPING
    }
    private isOpen mStatus=isOpen.CLOSE;//菜单状态
    private isAnimation mAnimationStatus=isAnimation.STOPPING;//动画状态
    private CardRotateMenuListener cardRotateMenuListener;

    public CardRotateMenu(Context context) {
        super(context);
    }

    public CardRotateMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CardRotateMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    //对菜单进行布局
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (changed) {
            for (int i = 0; i < getChildCount(); i++) {
                int width, height, iconWidth, iconHeight;
                width = getMeasuredWidth();
                height = getMeasuredHeight();
                View MenuIcon = getChildAt(i);
                iconWidth = MenuIcon.getMeasuredWidth();
                iconHeight = MenuIcon.getMeasuredHeight();
                if (i == 0) {
                    MainIcon = MenuIcon;
                    MainIcon.setOnClickListener(this);
                    MenuIcon.layout(width - iconWidth - 30, height - iconHeight - 30, width - 30, height - 30);
                } else {
                    MenuIcon.setVisibility(View.GONE);
                    MenuIcon.layout(width - iconWidth - 30, height - iconHeight - 30 - (i - 1) * (iconHeight + 30), width - 30, height - 30 - (i - 1) * (iconHeight + 30));
                    Log.i("BeginPosotion", MenuIcon.getLeft() + " " + MenuIcon.getTop() + " " + MenuIcon.getRight() + " " + MenuIcon.getBottom());
                }
            }
        }
    }

    //测量每一个View的大小
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //主按钮点击事件
    @Override
    public void onClick(View v) {
        if (getChildCount() > 1) {
            if (mAnimationStatus==isAnimation.STOPPING) {
                if (mStatus==isOpen.OPEN) {
                    closeAnimation(getChildCount() - 1);
                } else {
                    openAnimation(1);
                }
            }
        }
    }

    public interface CardRotateMenuListener {
        void MenuIconClick(View v);
    }

    public void setCardRotateMenuListener(CardRotateMenuListener cardRotateMenuListener) {
        this.cardRotateMenuListener = cardRotateMenuListener;
    }

    //菜单打开动画
    private void openAnimation(final int num) {
        mAnimationStatus=isAnimation.RUNNING;
        final View MenuIcon = getChildAt(num);
        MenuIcon.setPivotY(0);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(MenuIcon, "rotationX", 0.0F, 180.0F);
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(MenuIcon, "translationY", 0, -30);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(300);
        animatorSet.playTogether(objectAnimator, objectAnimator1);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                MenuIcon.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                MenuIcon.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mAnimationStatus==isAnimation.STOPPING&& cardRotateMenuListener != null) {
                            cardRotateMenuListener.MenuIconClick(v);
                            closeAnimation(getChildCount()-1);
                        }
                    }
                });
                if (num + 1 < getChildCount()) {
                    openAnimation(num + 1);
                } else {
                    mAnimationStatus=isAnimation.STOPPING;
                    mStatus=isOpen.OPEN;
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();
    }

    //菜单关闭动画
    private void closeAnimation(final int num) {
        mAnimationStatus=isAnimation.RUNNING;
        final View MenuIcon = getChildAt(num);
        MenuIcon.setPivotY(0);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(MenuIcon, "rotationX", 180F, 0.0f);
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(MenuIcon, "translationY", -30, 0);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(300);
        animatorSet.playTogether(objectAnimator, objectAnimator1);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (num - 1 > 0) {
                    MenuIcon.setVisibility(View.GONE);
                    closeAnimation(num - 1);
                } else {
                    MenuIcon.setVisibility(View.GONE);
                    mAnimationStatus=isAnimation.STOPPING;
                    mStatus=isOpen.CLOSE;
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();
    }
}
